package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.service.FileUploadService;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class FileUploadServiceImpl implements FileUploadService {


    //文件上传
    public String fileUpload(MultipartFile file) {

        String path = ClassUtils.getDefaultClassLoader().
                getResource("fdfs_client.conf").
                getPath();

        try {
            ClientGlobal.init(path);
            TrackerClient trackerClient = new TrackerClient();
            //连接Track
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;
            //连接上存储节点
            StorageClient1 storageClient1 = new StorageClient1();

            //文件扩展名
            String extensionName = FilenameUtils.getExtension(file.getOriginalFilename());
            //上传图片
            String imgUrl = storageClient1.upload_file1(file.getBytes(), extensionName, null);
            System.out.println(imgUrl);
            return imgUrl;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
